//
//  CYAllCategaryViewController.m
//  cyydt
//
//  Created by chu on 2019/2/3.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYAllCategaryViewController.h"
#import "CYAllCategary_LabelCollectionViewCell.h"
#import "CYAllCategaryCollectionReusableView.h"
#import "CYProjectCateViewController.h"

@interface CYAllCategaryViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@property (nonatomic, strong) NSMutableArray *cateArr;

@property (nonatomic, strong) NSArray *data;

@end

@implementation CYAllCategaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"全部分类";
    [self.view addSubview:self.collectionView];
    [self getCate];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CYProjectCateViewController *cate = [[CYProjectCateViewController alloc] init];
    cate.cateArr = self.data;
    CYCategrayModel *model = self.cateArr[indexPath.section];
    CYCategrayModel *childModel = model.child[indexPath.item];
    cate.param = @{@"type":@3, @"big_catid":model.catid, @"catid":childModel.catid};
    cate.title = childModel.catname;
    [[AppDelegate sharedAppDelegate] pushViewController:cate];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.cateArr.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    CYCategrayModel *model = self.cateArr[section];
    return model.child.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CYAllCategary_LabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CYAllCategary_LabelCollectionViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[CYAllCategary_LabelCollectionViewCell alloc] init];
    }
    CYCategrayModel *model = self.cateArr[indexPath.section];
    CYCategrayModel *childModel = model.child[indexPath.item];
    cell.titleLabel.text = childModel.catname;
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(80, 35);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeMake(kWindowW, 45);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 2) {
        //        return UIEdgeInsetsMake(20, 20, 20, 20);
    }
    return UIEdgeInsetsMake(10, 15, 10, 15);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *view = nil;
    CYCategrayModel *model = self.cateArr[indexPath.section];
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CYAllCategaryCollectionReusableView" forIndexPath:indexPath];
        CYAllCategaryCollectionReusableView *header = (CYAllCategaryCollectionReusableView *)view;
        [header.headImageView sd_setImageWithURL:[NSURL URLWithString:model.icon]];
        header.titleLabel.text = model.catname;
        return header;
    }
    return view;
}

- (void)getCate{
    NSString *url = [NSString stringWithFormat:@"%@%@",HOST, @"cate-gory/item"];
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:nil ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"分类列表 ---- %@",responseResult);
        if ([self.collectionView.mj_header isRefreshing]) {
            [self.collectionView.mj_header endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSArray *data = responseResult[@"data"];
                self.data = data;
                [self.cateArr removeAllObjects];
                for (NSDictionary *dic in data) {
                    CYCategrayModel *model = [CYCategrayModel modelWithDictionary:dic];
                    [self.cateArr addObject:model];
                }
                [self.collectionView reloadData];
            }else{
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        //居左约束
        UICollectionViewFlowLayout *leftAlignedLayout = [[UICollectionViewFlowLayout alloc] init];
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar) collectionViewLayout:leftAlignedLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"CYAllCategary_LabelCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CYAllCategary_LabelCollectionViewCell"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"CYAllCategaryCollectionReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CYAllCategaryCollectionReusableView"];
        
    }
    return _collectionView;
}

- (NSMutableArray *)cateArr{
    if (!_cateArr) {
        _cateArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _cateArr;
}
@end
